package com.fuxi.cloud.desreport.render.utils;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.fuxi.cloud.desreport.render.b.b;
import com.fuxi.cloud.desreport.render.handler.data.GroupHandler;
import com.fuxi.cloud.desreport.render.handler.data.RecursionHandler;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * ReportDataUtil
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */
public class ReportDataUtil {
    public ReportDataUtil() {
    }

    public static Map<String, Object> a(List<Map<String, Object>> var0, List<b> var1) {
        b var2 = (b)var1.get(0);
        Object var3 = a(var0, var2);
        HashMap var4 = new HashMap(5);
        if (var1.size() > 1) {
            b var5 = (b)var1.get(1);
            Iterator var6 = ((Map)var3).entrySet().iterator();

            while(var6.hasNext()) {
                Map.Entry var7 = (Map.Entry)var6.next();
                Object var8 = var7.getKey();
                List var9 = (List)var7.getValue();
                Map var10 = a(var9, var5);
                var2.getChildSize().add(var10.size());
                var4.put(var8.toString(), var10);
            }

            var3 = var4;
            HashMap var17 = new HashMap(5);
            if (var1.size() > 2) {
                Iterator var18 = var4.entrySet().iterator();

                while(true) {
                    Object var20;
                    Object var21;
                    do {
                        if (!var18.hasNext()) {
                            var3 = var17;
                            if (var1.size() > 3) {
                                ;
                            }

                            return (Map)var3;
                        }

                        Map.Entry var19 = (Map.Entry)var18.next();
                        var20 = var19.getKey();
                        var21 = var19.getValue();
                    } while(!(var21 instanceof HashMap));

                    HashMap var11 = new HashMap(5);
                    Map var12 = (Map)var21;
                    Iterator var13 = var12.entrySet().iterator();

                    while(var13.hasNext()) {
                        Map.Entry var14 = (Map.Entry)var13.next();
                        List var15 = (List)var14.getValue();
                        Map var16 = a(var15, (b)var1.get(2));
                        var5.getChildSize().add(var16.size());
                        var11.put(var14.getKey().toString(), var16);
                    }

                    var17.put(var20.toString(), var11);
                }
            }
        }

        return (Map)var3;
    }

    private static Map<String, Object> a(List<Map<String, Object>> var0, b var1) {
        LinkedHashMap var2 = new LinkedHashMap();
        String var3 = var1.getColumn();
        List var4 = var1.getValues();

        for(int var5 = 0; var5 < var0.size(); ++var5) {
            Map var6 = (Map)var0.get(var5);
            List var7 = (List)var2.get(var6.get(var3));
            if (var7 == null) {
                ArrayList var9 = new ArrayList();
                var9.add(var6);
                String var8 = var6.get(var3).toString();
                var4.add(var8);
                var2.put(var6.get(var3).toString(), var9);
            } else {
                var7.add(var6);
            }
        }

        var1.setValues(var4);
        return var2;
    }

    public static Map b(List<Map<String, Object>> var0, List<b> var1) {
        if (!ObjectUtil.isNotEmpty(var1)) {
            return null;
        } else {
            LinkedList var2 = new LinkedList(var1);
            Map var3 = (Map)var0.stream().collect(Collectors.groupingBy((var1x) -> {
                return var1x.get(((b)var2.get(0)).getColumn());
            }));
            var2.remove(0);
            Iterator var4 = var2.iterator();

            while(var4.hasNext()) {
                b var5 = (b)var4.next();
                a(var3, var5);
            }

            return var3;
        }
    }

    public static void a(Map var0, b var1) {
        a(var0, (b)var1, (GroupHandler)null, (Boolean)false);
    }

    public static void a(Map var0, b var1, GroupHandler var2, Boolean var3) {
        var0.forEach((var3x, var4) -> {
            if (var4 instanceof ArrayList) {
                List<JSONObject> var5 = (List)var4;

                try {
                    Map var6 = (Map)var5.stream().filter((var1x) -> {
                        return ObjectUtil.isNotEmpty(var1x.getString(var1.getColumn()));
                    }).collect(Collectors.groupingBy((var1x) -> {
                        return var1x.get(var1.getColumn());
                    }));
                    var0.put(var3x, var6);
                } catch (Exception var7) {
                    var7.printStackTrace();
                }
            } else {
                a((Map)var4, var1, var2, false);
            }

        });
        if (ObjectUtil.isNotEmpty(var2) && var3) {
            var2.handler(var0, var1);
        }

    }

    public static List<Map<String, Object>> c(List<Map<String, Object>> var0, List<b> var1) {
        b var3 = null;
        b finalVar = var3;
        for(Iterator var2 = var1.iterator(); var2.hasNext(); var0 = (List)var0.stream().sorted(Comparator.comparing((var1x) -> {
            return var1x.get(finalVar.getColumn()).toString();
        })).collect(Collectors.toList())) {
            var3 = (b)var2.next();
        }

        return var0;
    }

    public static void a(Map var0, RecursionHandler var1, JSONObject var2, String var3) {
        AtomicReference var4 = new AtomicReference(new JSONObject());
        var0.forEach((var4x, var5) -> {
            String var6 = var4x.toString();
            if (var5 instanceof ArrayList) {
                List var7 = (List)var5;
                String var8 = var3;
                if (ObjectUtil.isNotEmpty(var3)) {
                    var8 = var3 + "_";
                }

                var7 = var1.doList(var6, var7, var2, var8 + var6);
                Iterator var9 = var7.iterator();

                while(var9.hasNext()) {
                    JSONObject var10 = (JSONObject)var9.next();
                    JSONObject var11 = var1.doObj(var6, var10, var2);
                    if (ObjectUtil.isNotEmpty(var2)) {
                        var11.putAll(var2);
                    }
                }
            } else {
                if (ObjectUtil.isNotEmpty(var3)) {
                    var6 = var3 + "_" + var4x.toString();
                }

                new LinkedList();
                var4.set(var1.doMap(var6, (Map)var5, (JSONObject)var4.get()));
                a((Map)var5, var1, (JSONObject)var4.get(), var6);
            }

        });
    }

    public static void main(String[] args) {
    }
}
